using SharedKernel.Domain;
using System;

namespace OrderService.Domain.Models
{
    /// <summary>
    /// 订单项实体
    /// </summary>
    public class OrderItem : CreateUpdateDeleteEntity<Guid>
    {
        /// <summary>
        /// 商品ID
        /// </summary>
        public Guid ProductId { get; private set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string ProductName { get; private set; }

        /// <summary>
        /// 单价
        /// </summary>
        public decimal UnitPrice { get; private set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Quantity { get; private set; }

        /// <summary>
        /// 行总计
        /// </summary>
        public decimal LineTotal => UnitPrice * Quantity;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">订单项ID</param>
        /// <param name="productId">商品ID</param>
        /// <param name="productName">商品名称</param>
        /// <param name="unitPrice">单价</param>
        /// <param name="quantity">数量</param>
        private OrderItem(Guid id, Guid productId, string productName, decimal unitPrice, int quantity)
            : base(id)
        {
            ProductId = productId;
            ProductName = productName;
            UnitPrice = unitPrice;
            Quantity = quantity;
        }

        /// <summary>
        /// 创建订单项
        /// </summary>
        /// <param name="productId">商品ID</param>
        /// <param name="productName">商品名称</param>
        /// <param name="unitPrice">单价</param>
        /// <param name="quantity">数量</param>
        /// <returns>订单项实例</returns>
        public static OrderItem Create(Guid productId, string productName, decimal unitPrice, int quantity)
        {
            if (string.IsNullOrWhiteSpace(productName))
                throw new ArgumentNullException(nameof(productName), "商品名称不能为空");

            if (quantity <= 0)
                throw new ArgumentException("商品数量必须大于0", nameof(quantity));

            if (unitPrice < 0)
                throw new ArgumentException("商品单价不能小于0", nameof(unitPrice));

            return new OrderItem(Guid.NewGuid(), productId, productName, unitPrice, quantity);
        }
    }
}